<?php
class Commonmodel extends Model {
	function Commonmodel(){
		parent::Model();
	}
	function insert($tablename,$data){
		$this->db->insert($tablename,$data);
	}
	//get的时候没有条件
	function get($tablename){
		$query=$this->db->get($tablename);
		return $query;
	}
	//get的时候有一个条件
	function get_where($tablename,$field,$value){
		$this->db->where($field,$value);
		$query=$this->db->get($tablename);
		return $query;
	}
	//get的时候需要排序
	function get_orderby($tablename,$orderby){
		$this->db->order_by($orderby);
		$query=$this->db->get($tablename);
		return $query;
	}
	function delete($tablename,$field,$value){
		$this->db->where($field,$value);
		$query=$this->db->delete($tablename);
		return $query;
	}
	function update($tablename,$data,$field,$value){
		$this->db->where($field,$value);
		$this->db->update($tablename,$data);
	}
	function page($tablename,$per_nums,$start_position){//传入3个参数，表名字，每页的数据量，其实位置
		$this->db->limit($per_nums,$start_position);
		$query=$this->db->get($tablename);
		$data=$query->result();
		$data2[]=$data;//这里大家可能看的有点不明白，可以分别将$data和$data2打印出来看看是什么结果。
		return $data2;
	}
	function page2($sql){
		$data=$this->sql_self($sql);
		return $data;
	}
	function page_where($tablename,$per_nums,$start_position,$field,$value){
		$this->db->limit($per_nums,$start_position);
		$this->db->where($field,$value);
		$query=$this->db->get($tablename);
		$data=$query->result();
		$data2[]=$data;
		return $data2;
	}
	function getsort($tablename){
		$query=$this->db->get($tablename);
		foreach ($query->result() as $row ){
			$data[]=$row;
		}
		return $data;
	}
	function getchildren ($array,$pid=0,$y,&$tdata=array()){
		foreach ($array as $value){
			if($value->pid==$pid){
				$n = $y + 1;
				$value->deep = $y;
				$tdata[]=$value;
				$this->getChildren($array,$value->sortid,$n,$tdata);
			}
		}
		return $tdata;
	}
	function check($tablename,$field1,$var1,$field2,$var2){
		$this->db->where($field1,$var1);
		$this->db->where($field2,$var2);
		$query=$this->db->get($tablename);
		return $query->result();
	}
	function nums($tablename){
		$query=$this->db->count_all($tablename);
		return $query;
	}
	function nums_where($tablename,$field,$value){
		$sql='select count(*) as nums from '.$tablename.' where '.$field.'='.$value;
		//echo $sql;
		//exit;
		$query=$this->sql_self($sql);
		return $query[0]['nums'];
	}
	function sql_self($sql){
		$query=$this->db->query($sql);
		return $query->result_array();
	}
	function sql_self_object($sql){
		$query=$this->db->query($sql);
		return $query->result();
	}
}
?>